package com.meiya.hugegraph.util;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.meiya.hugegraph.config.HugeGraphConfig;

/**
 * RESTful API工具类
 * 提供HTTP请求相关的工具方法
 */
@Component
public class RestUtil {
    
    private static final Logger logger = LoggerFactory.getLogger(RestUtil.class);
    private static final RestTemplate restTemplate = new RestTemplate();
    private static HugeGraphConfig config;
    
    /**
     * 注入配置
     * 
     * @param hugeGraphConfig HugeGraph配置
     */
    @Autowired
    public void setConfig(HugeGraphConfig hugeGraphConfig) {
        config = hugeGraphConfig;
    }
    
    /**
     * 发送POST请求
     * 
     * @param url 请求URL
     * @param requestBody 请求体
     * @param responseType 返回类型的Class
     * @param <T> 返回类型
     * @return 响应结果
     */
    public static <T> T post(String url, Object requestBody, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        
        HttpEntity<Object> entity = new HttpEntity<>(requestBody, headers);
        
        logger.debug("发送POST请求到: {}", url);
        ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
        
        return response.getBody();
    }
    
    /**
     * 批量添加顶点
     * 
     * @param vertices 顶点数据列表
     * @return 响应结果
     */
    public static String batchAddVertices(List<Map<String, Object>> vertices) {
        String serverUrl = config != null ? config.getServerUrl() : "http://localhost:8080";
        String graphName = config != null ? config.getGraphName() : "hugegraph";
        return batchAddVertices(serverUrl, graphName, vertices);
    }
    
    /**
     * 批量添加顶点
     * 
     * @param serverUrl 服务器URL
     * @param graphName 图名称
     * @param vertices 顶点数据列表
     * @return 响应结果
     */
    public static String batchAddVertices(String serverUrl, String graphName, 
                                          List<Map<String, Object>> vertices) {
        String url = String.format("%s/graphs/%s/graph/vertices/batch", serverUrl, graphName);
        return post(url, vertices, String.class);
    }
    
    /**
     * 批量添加边
     * 
     * @param edges 边数据列表
     * @param checkVertex 是否检查顶点存在
     * @return 响应结果
     */
    public static String batchAddEdges(List<Map<String, Object>> edges, boolean checkVertex) {
        String serverUrl = config != null ? config.getServerUrl() : "http://localhost:8080";
        String graphName = config != null ? config.getGraphName() : "hugegraph";
        return batchAddEdges(serverUrl, graphName, edges, checkVertex);
    }
    
    /**
     * 批量添加边
     * 
     * @param serverUrl 服务器URL
     * @param graphName 图名称
     * @param edges 边数据列表
     * @param checkVertex 是否检查顶点存在
     * @return 响应结果
     */
    public static String batchAddEdges(String serverUrl, String graphName, 
                                       List<Map<String, Object>> edges, boolean checkVertex) {
        String url = String.format("%s/graphs/%s/graph/edges/batch?check_vertex=%s", 
                                  serverUrl, graphName, checkVertex);
        return post(url, edges, String.class);
    }
} 